/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
$(function () {
    var totalmoney = 0.00;
    $(".commo_each_total").each(function () {
        var each = parseFloat($(this).text());
        totalmoney = parseFloat(totalmoney) + each;
    });

    $("#all_checked").click(function () {
        var src = $("#coll_checkbox").attr("src");
        if (src === "/img/user/checked.png") {
            $("#coll_checkbox").attr("src", "/img/user/checkbox.png");
            $(".coll_checkbox_each").attr("src", "/img/user/checkbox.png");
        } else if (src === "/img/user/checkbox.png") {
            $("#coll_checkbox").attr("src", "/img/user/checked.png");
            $(".coll_checkbox_each").attr("src", "/img/user/checked.png");
        } else {
            $("#coll_checkbox").attr("src", src);
            $(".coll_checkbox_each").attr("src", "/img/user/checked.png");
        }

    });
    $("#coll_checkbox").click(function () {
        var src = $("#coll_checkbox").attr("src");
        if (src === "/img/user/checked.png") {
            $("#coll_checkbox").attr("src", "/img/user/checkbox.png");
            $(".coll_checkbox_each").attr("src", "/img/user/checkbox.png");
        } else if (src === "/img/user/checkbox.png") {
            $("#coll_checkbox").attr("src", "/img/user/checked.png");
            $(".coll_checkbox_each").attr("src", "/img/user/checked.png");
        } else {
            $("#coll_checkbox").attr("src", src);
            $(".coll_checkbox_each").attr("src", "/img/user/checked.png");
        }

    });
    $(".coll_checkbox_each").click(function () {
        var src = $(this).attr("src");
        if (src === "/img/user/checked.png") {
            $(this).attr("src", "/img/user/checkbox.png");
        } else if (src === "/img/user/checkbox.png") {
            $(this).attr("src", "/img/user/checked.png");
        } else {
            $(this).attr("src", "/img/user/checkbox.png");
        }
    });
    $(".fa-minus-square-o").click(function () {
        var num = $(this).parent("td").children("input").val();
        var anum = 1;
        var tnum = 0.00;
        if (parseInt(num) > 1) {
            anum = parseInt(num) - 1;
            $(this).parent("td").children("input").val(anum);
        } else {
            anum = 1;
            $(this).parent("td").children("input").val(1);
        }
        var price = $(this).parent("td").prev("td").children("label").text();
        var p = parseFloat(price);
        var total = parseInt(anum) * p;
        $(this).parent("td").next("td").children("label").text(total.toFixed(2));
        $(".commo_each_total").each(function () {
            var each = parseFloat($(this).text());
            tnum = parseFloat(tnum) + parseFloat(each);
        });
        $(".all_commo_price").text(tnum.toFixed(2));
    });
    $(".fa-plus-square-o").click(function () {
        var num = $(this).parent("td").children("input").val();
        var anum = 1;
        var tnum = 0.00;
        if (parseInt(num) >= 10) {
            anum = 10;
            $(this).parent("td").children("input").val(10);
        } else {
            anum = parseInt(num) + 1;
            $(this).parent("td").children("input").val(anum);
        }
        var price = $(this).parent("td").prev("td").children("label").text();
        var p = parseFloat(price);
        var total = parseInt(anum) * p;
        $(this).parent("td").next("td").children("label").text(total.toFixed(2));
        $(".commo_each_total").each(function () {
            var each = parseFloat($(this).text());
            tnum = parseFloat(tnum) + parseFloat(each);
        });
        $(".all_commo_price").text(tnum.toFixed(2));
    });
    $(".commo_num").keyup(function () {
        var num = $(this).val();
        var tnum = 0.00;
        if (parseInt(num) > 10) {
            alert("不能超过10");
            num = 10;
            $(this).val(10);
        }
        if (parseInt(num) === 0 || parseInt(num) < 0) {
            alert("不能低于1");
            num = 1;
            $(this).val(1);
        }
        if (num === undefined || num === "") {
            alert("不能低于1");
            num = 1;
            $(this).val(1);
        }
        var price = $(this).parent("td").prev("td").children("label").text();
        var p = parseFloat(price);
        var total = parseInt(num) * p;
        $(this).parent("td").next("td").children("label").text(total.toFixed(2));
        $(".commo_each_total").each(function () {
            var each = parseFloat($(this).text());
            tnum = parseFloat(tnum) + parseFloat(each);
        });
        $(".all_commo_price").text(tnum.toFixed(2));
    });
    $(".all_commo_price").text(totalmoney.toFixed(2));
    $("#all_checked").mouseover(function () {
        $(this).css("color", "#e31f4a");
    });
    $("#all_checked").mouseleave(function () {
        $(this).css("color", "#000");
    })
    $("#delete_shopcar_all").mouseover(function () {
        $(this).css("color", "#e31f4a");
    });
    $("#delete_shopcar_all").mouseleave(function () {
        $(this).css("color", "#000");
    });
    $(".shopcar_delete").mouseover(function () {
        $(this).css("color", "#e31f4a");
    });
    $(".shopcar_delete").mouseleave(function () {
        $(this).css("color", "#000");
    });
    $(".shopcar_delete").click(function(){
       var cartid = $(this).parent("td").siblings("td").find(".hiddenCartId").val();
       $.ajax({
            type: "post",
            url: "/deleteCart.do",
            timeout: 1000,
            data: {ids: cartid},
            dataType: "text",
            cache: false,
            success: function (data) {
                if (data === "success") {
                    alert("删除购物车成功!");
                    location.reload(); 
                } else {
                    alert("删除购物车失败!");
                    
                }
            }
        });
    });
    
    $("#delete_shopcar_all").click(function(){
        var cartids = "";
        $(".order_list_info_tr").find(".coll_checkbox_each").each(function(){
            var src = $(this).attr("src");
            if(src === "/img/user/checked.png"){
               var cartid = $(this).prev("input").val();
               cartids += cartid + ",";
            }
        });
        $.ajax({
            type: "post",
            url: "/deleteCart.do",
            timeout: 1000,
            data: {ids: cartids},
            dataType: "text",
            cache: false,
            success: function (data) {
                if (data === "success") {
                    alert("删除购物车成功!");
                    location.reload();  
                } else {
                    alert("删除购物车失败!");
                    
                }
            }
        });
    });
});

